package com.jservice.api.service.upp;

import com.alibaba.fastjson.JSONObject;
import com.jservice.api.MsgCode;
import com.jservice.api.UppClient;
import com.jservice.api.dto.Request;
import com.jservice.api.dto.Response;
import com.jservice.api.dto.ResponseHeader;
import com.jservice.api.service.BaseService;
import com.jservice.api.service.IService;
import com.jservice.api.smkAppUser.AppUserKit;
import com.jservice.model.SmkAppUser;
import com.jservice.web.smkSn.SnService;
import com.wondersgroup.upp.api.core.UPPlatformRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Upp2007 - 账户提现
 *
 * @Author cwledit
 * @DATE 2017/6/14.
 */
@Service("upp2007")
public class Upp2007 extends BaseService implements IService {

    private String SERVER_NAME = "upp.trade.withdrawal";

    @Resource
    private SnService snService;

    @Override
    public Response excute(Request request, Response response, ResponseHeader header, String method) throws IOException {
        Map paramMap = request.getData();
        String card_no = paramsStringFilter(paramMap.get("card_no"));
        String amount = paramsStringFilter(paramMap.get("amount"));
        String bank_card = paramsStringFilter(paramMap.get("bank_card"));
        SmkAppUser smkAppUser = AppUserKit.getCurrent(request);
        if (smkAppUser == null) {
            return renderFAIL(MsgCode.Code_1007, response, header);
        }
        Map data = new HashMap();
        data.put("card_no", card_no);
        data.put("amount", amount);//单位‘分’
        data.put("out_trade_no", snService.generate(SnService.Type.withdrawal));//商户订单号
        data.put("account_type", "06");
        data.put("auth_type", "032");
        data.put("withdrawal_type", "02");
        data.put("bank_card", bank_card);

        UPPlatformRequest uRequest = getUPRequest(SERVER_NAME, request.getHeader());
        uRequest.setBiz_content(sortParam(data));
        UppClient client = UppClient.getInstance();
        JSONObject jsonObject = client.doRequest(uRequest);
        if ("000000".equals(jsonObject.getString("code"))) {
            jsonObject.remove("code");
            return renderSUC(jsonObject, response, header);
        } else {
            return renderFAIL(MsgCode.Code_1009, "市民卡网关返回异常:" + jsonObject.getString("msg"), response, header);
        }
    }
}
